﻿package zf.data.geom.plan
{
	import flash.display.Sprite;
	import flash.geom.Point;

	/**
	 * 正星形模型类
	 * @author Mike.zhang
	 * @langversion 3.0 
	 * @playerversion Flash 9 
	 * 
	 */
	public class RegularStar extends Plan implements IPlan
	{
		private var _corner:uint;
		private var _innerRadius:Number;
		private var _outerRadius:Number;

		/**
		 * 创建一个正星形模型
		 * @param corner 正n星形的n值
		 * @param innerRadius 内半径
		 * @param outerRadius 外半径
		 * 
		 */
		public function RegularStar (corner:uint=3,innerRadius:Number=20,outerRadius:Number=50)
		{
			super ();
			_corner=corner;
			_innerRadius=innerRadius;
			_outerRadius=outerRadius;
		}
		/**
		 * 正n星形的n值。请注意，正n星形其实有2n个顶点，2n个边构成。之所以不同是因为它表示的是正n星形的n值。例如正5星形其实是正五角星 
		 * @return 正n星形的n值
		 * 
		 */
		public function get corner ():uint
		{
			return _corner;
		}

		public function set corner (v:uint):void
		{
			_corner=v;
		}
		/**
		 * 外半径长度值 
		 * @return 外半径
		 * 
		 */
		public function get outerRadius ():Number
		{
			return _outerRadius;
		}

		public function set outerRadius (v:Number):void
		{
			_outerRadius=v;
		}

		/**
		 * 内半径长度值 
		 * @return 内半径
		 * 
		 */
		public function get innerRadius ():Number
		{
			return _innerRadius;
		}

		public function set innerRadius (v:Number):void
		{
			_innerRadius=v;
		}

		/**
		 * 获取正星形的顶点数组集。
		 * 
		 * @inheritDoc
		 * @return 正n星形顶点数组
		 * @throws ArgumentError corner必须大于2，内外半径必须为数字
		 * 
		 */
		public function getPointData (x:Number=0,y:Number=0,rotation:Number=0):Array
		{
			//@copy Plan#getPointData()
			if (_corner > 2 && _innerRadius is Number && _outerRadius is Number)
			{
				var data:Array=new Array  ;

				var step:Number=Math.PI * 2 / _corner;
				var halfStep:Number=step / 2;
				var start:Number=rotation / 180 * Math.PI;
				var dx:Number;
				var dy:Number;
				//this.graphics.moveTo(x+Math.cos(start)*outerRadius,y-Math.sin(start)*outerRadius);

				for (var n:uint=0; n < _corner; n++)
				{
					dx=x + Math.cos(start + step * n - halfStep) * _innerRadius;
					dy=y - Math.sin(start + step * n - halfStep) * _innerRadius;
					data.push (new Point(dx,dy));
					//this.graphics.lineTo(dx,dy);
					dx=x + Math.cos(start + step * n) * _outerRadius;
					dy=y - Math.sin(start + step * n) * _outerRadius;
					data.push (new Point(dx,dy));
					//this.graphics.lineTo(dx,dy);
				}
				return data;
			}
			else
			{
				throw new ArgumentError("正星形参数错误！");
			}
			return new Array  ;
		}
		/**
		 * 绘制正星形
		 * 在使用前需要设置填充及线条信息。 
		 * 
		 * @inheritDoc
		 * @throws ArgumentError corner必须大于2，内外半径必须为数字
		 * @example 下面例子说明如何绘制一个正3星形,其外半径50px，内半径20px
		 * <listing version="3.0">
		 * import zf.data.geom.plan.RegularStar;
		 * import flash.display.Sprite;
		 * 
		 * var myRegularStar:RegularStar=new RegularStar;
		 * var sp:Sprite=new Sprite;
		 * sp.graphics.beginFill (0x000000,1);
		 * sp.graphics.lineStyle (1,0xFF0000);
		 * myRegularStar.draw(sp);
		 * sp.graphics.endFill (); 
		 * this.addChild(sp);
		 * </listing>
		 * 
		 */
		override public  function draw (target:Sprite,x:Number=0,y:Number=0,rotation:Number=0):void
		{
			//@copy Plan#draw()
			if (_corner > 2 && _innerRadius is Number && _outerRadius is Number)
			{

				var step:Number=Math.PI * 2 / _corner;
				var halfStep:Number=step / 2;
				var start:Number=rotation / 180 * Math.PI;
				var dx:Number;
				var dy:Number;
				target.graphics.moveTo (x + Math.cos(start) * outerRadius,y - Math.sin(start) * outerRadius);

				for (var n:uint=1; n <= _corner; n++)
				{
					dx=x + Math.cos(start + step * n - halfStep) * _innerRadius;
					dy=y - Math.sin(start + step * n - halfStep) * _innerRadius;

					target.graphics.lineTo (dx,dy);
					dx=x + Math.cos(start + step * n) * _outerRadius;
					dy=y - Math.sin(start + step * n) * _outerRadius;

					target.graphics.lineTo (dx,dy);
				}
			}
			else
			{
				throw new ArgumentError("正星形参数错误！");
			}
		}
	}
}